<template>
  <div class="app-container">
    <div class="action-container" />
    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="线索编码" v-model="listQuery.f_lk_clue$serial"></el-input>
      <el-input @keyup.enter.native="handleFilter" style="width: 200px; margin-left: 10px;" class="filter-item" placeholder="被反映人" v-model="listQuery.f_lk_clue$respondent"></el-input>
      <el-select v-model="listQuery.f_eq_organization$parentId" style="width: 150px; margin-left: 10px;" class="filter-item" placeholder="办理机关一级" @change='onOrgList1Changed'>
        <el-option label="全部(一级机关）" value="" />
        <el-option v-for="item in orgList1" :key="item.id" :label="item.name" :value="item.id"></el-option>
      </el-select>
      <el-select v-model="listQuery.f_eq_orgId" style="width: 150px; margin-left: 10px;" class="filter-item" placeholder="办理机关二级" @change="handleFilter">
        <el-option label="全部(二级机关）" value="" />
        <el-option v-for="item in orgList2" :key="item.id" :label="item.name" :value="item.id"></el-option>
      </el-select>
      <el-select @change='handleFilter' style="width: 120px; margin-left: 10px;" class="filter-item" v-model="listQuery.f_eq_action" placeholder="办理进度">
        <el-option label="全部" value="" />
        <el-option label="处置中" value="1" />
        <el-option label="办结中" value="2" />
      </el-select>
      <el-button class="filter-item" type="success" icon="el-icon-search" @click="resetFiler">重置</el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
    </div>

    <el-table :data="list" v-loading.body="listLoading" element-loading-text="数据正在加载中，请稍后..." border fit highlight-current-row>
      <el-table-column align="center" label='id' width="80">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="线索编码" width="150">
        <template slot-scope="scope">
          {{ (scope.row.clue || {}).serial }}
        </template>
      </el-table-column>
      <el-table-column label="被反映人" width="150">
        <template slot-scope="scope">
          {{ (scope.row.clue || {}).respondent }}
        </template>
      </el-table-column>
      <el-table-column label="责任机关" width="150">
        <template slot-scope="scope">
          {{ (scope.row.organization || {}).name }}
        </template>
      </el-table-column>
      <el-table-column label="办理进度" width="150">
        <template slot-scope="scope">
          {{ scope.row.action | actionValueFilter }}
        </template>
      </el-table-column>
      <el-table-column label="开始时间">
        <template slot-scope="scope">
          {{ scope.row.startTime }}
        </template>
      </el-table-column>
      <el-table-column label="类别">
        <template slot-scope="scope">
          <el-tag :type="scope.row.category | categoryClassFilter">{{ scope.row.category }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="预警/逾期时间（天）">
        <template slot-scope="scope">
          {{ scope.row.exceedDays }}
        </template>
      </el-table-column>
    </el-table>

    <div v-show="!listLoading" class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page"
                     :page-sizes="[10,20,30,50]" :page-size="listQuery.size" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getClueAlertList } from '@/api/clue'
import { getOrgList } from '@/api/org'

export default {
    data() {
      return {
        orgList1: [],
        orgList2: [],
        list: null,
        listLoading: true,
        total: null,
        listQuery: {
          page: 1,
          size: 20,
          f_lk_clue$serial: '',
          f_lk_clue$respondent: '',
          f_eq_organization$parentId: '',
          f_eq_orgId: '',
          f_eq_action: '',
          f_eq_status: 0
        }
      }
    },
  filters: {
    actionValueFilter(status) {
      const actionMap = {
        1: '处置中',
        2: '办结中'
      }

      return actionMap[status]
    },
    categoryClassFilter(status) {
      const statusMap = {
        '预警': 'warning',
        '逾期': 'danger'
      }
      return statusMap[status]
    }
    },
    created() {
      //查询数据
      this.fetchOrgList1()
      this.fetchData()
    },
    methods: {
      fetchOrgList1() {
        getOrgList({ f_eq_type: 1 }).then(response => {
          this.orgList1 = response.data
        })
      },
      fetchOrgList2(parentId) {
        getOrgList({ f_eq_parentId: parentId }).then(response => {
          this.orgList2 = response.data
        })
      },
      onOrgList1Changed() {
        if (this.listQuery.f_eq_organization$parentId !== '') {
          this.fetchOrgList2(this.listQuery.f_eq_organization$parentId)
        } else {
          this.orgList2 = []
        }
        // 一级部门改变后，默认二级部门默认全选
        this.listQuery.f_eq_orgId = ''
        this.listQuery.page = 1
        this.fetchData()
      },
      fetchData() {
        this.listLoading = true
        getClueAlertList(this.listQuery).then(response => {
          this.listLoading = false
          this.list = response.data
          for (const row of this.list) {
            if (row.exceedDays >= 0) {
              row.category = '预警'
            } else {
              row.category = '逾期'
              row.exceedDays = Math.abs(row.exceedDays)
            }
          }
          this.total = response.total
        }).catch(() => {
          this.listLoading = false
        })
      },
      handleFilter() {
        this.listQuery.page = 1
        this.fetchData()
      },
      resetFiler() {
        this.listQuery.f_lk_clue$serial = ''
        this.listQuery.f_lk_clue$respondent = ''
        this.listQuery.f_eq_organization$parentId = ''
        this.listQuery.f_eq_orgId = ''
        this.listQuery.f_eq_action = ''
        this.listQuery.page = 1
        this.listQuery.size = 20
        this.fetchData()
      },
      handleSizeChange(val) {
        this.listQuery.size = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.fetchData()
      }
    }
  }
</script>
